﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace WPFExcelReport
{

    public partial class TZZRULES
    {
        #region ==KETTART===========
        public static readonly DependencyProperty KETTARTProperty = DependencyProperty.Register("KETTART",
        typeof(string), typeof(TZZRULES),
        new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnKETTARTChanged));
        public string KETTART
        {
            get
            {
                return (string)GetValue(KETTARTProperty);
            }
            set
            {
                SetValue(KETTARTProperty, value);
                OnKETTARTChg();
            }
        }
        private static void OnKETTARTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TZZRULES;
            if (src != null)
            {
                src.KETTART = (string)e.NewValue;
            }
        }
        public virtual void OnKETTARTChg()
        {
            if (TMV == null)
                return;
            TMV.UMS = KETTART;
        }
        #endregion
        public TZZRULES()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            InitializeComponent();
        }
        public override void Reset()
        {
            base.Reset();
        }
        private void btnResetP_Click(object sender, RoutedEventArgs e)
        {
            Reset();
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            CHKFLOW();
        }
        public void CHKFLOW()
        {
            if (dwMaster == null)
                return;
            string ARTSTAGE = dwMaster["ARTSTAGE"].ToString();
            FLOW = "";
            FLOW = dwMaster["UNDES"].ToString();
            if (FLOW != "")
            {
                return;
            }
            switch (app.company)
            {               
                default:
                case "SXN":
                case "UBT":
                    cAP02.vwMaster.View.MoveCurrentTo(-1);
                    cAP02.vwMaster.View.MoveCurrentToFirst();
                    break;
            }
        }
        
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
             
            TMV.UpdateICC_PRULES(dwMaster, 0);
 
            MessageInfo msg = new MessageInfo();
            msg.Sender = this.RUNID;
            msg.MSGTYPE = "ZZABPNEW";
            msg.ACTION = "ZZABPNEW";
            msg.AUFTYP = "V";
            msg.WAKZ = "R";
            msg.ART = RLPRART.Substring(0, 20);
            msg.VAR = RLPRART.Substring(20, 2);
            msg.FARBE = RLPRART.Substring(23, 10);
            msg.ORDER = (string)PM["ORDERNUM"];
            msg.Where = app.User;
            VICC_WFEVENTS.SVWF.CheckEvents("UIMESSAGE", null, "", msg);
            base.SaveCommand(sender, e);
        }
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if(!IsNew)
                {
                    IsNew = true;
                    return;
                }
                if (KRS == "")
                {
                    throw new Exception("前缀");
                }
                if (RLPRART == "")
                {
                    throw new Exception("品种");
                }
                if (FLOW == "")
                {
                    throw new Exception("流程");
                }
 
                TBMain.Clear();
                dwData = TBMain.NewRow();

                PM["RUNID"] = this.RUNID;
                PM["KRS"] = KRS;
                PM["AUFART"] = "PR";
                PM["ORDERNUM"] = TMV.date;
                PM["RELOADABK"] = false;
                string ABAUF = TMV.VABK.NEWABK(PM);
                LFD = ABAUF.Substring(2, 6);
                POS = VSDDAT.SD22.GETNEXT("0022", KRS, LFD, 1, 3);
                PM["ORDERNUM"] = ABAUF.Substring(0, 2) + LFD + POS + "PR ";
                PM["COLOR"] = "PR ";
                PM["AUFTYP"] = "V";
                PM["ARTSTAGE"] = "Z";
                //PM["VKAUF"] = pm["ORDERNUM"];
                PM["VKPAUF"] = KRS+ LFD + POS;
                PM["VKPQTY"] =0;//
                PM["VKAUF"] = "                  ";//
                PM["UNDES"] = FLOW;
                PM["DELDATE"] = TMV.date;
                PM["STARTDATE"] = TMV.date; 
                TMV.NEWPLAN(PM);
                vwMaster.View.MoveCurrentToFirst();
                IsNew = false;
                base.NewCommand(sender, e);
                SaveCommand(sender, e); 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
